﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;

namespace codeding.KMeans.DataStructures.Lists
{
    public class Movie : DoiTuong
    {
        #region int MovieID property
        public int MovieID
        {
            get
            {
                if (_DataRow["MovieID"] is int) return (int)_DataRow["MovieID"];
                return -1;
            }
            set
            {
                _DataRow["MovieID"] = value;
            }
        }
        #endregion

        #region string MovieName property
        public string MovieName
        {
            get
            {
                if (_DataRow["MovieName"] is string) return (string)_DataRow["MovieName"];
                return "";
            }
            set
            {
                _DataRow["MovieName"] = value;
            }
        }
        #endregion

        #region int Year property
        public int Year
        {
            get
            {
                if (_DataRow["Year"] is int) return (int)_DataRow["Year"];
                return -1;
            }
            set
            {
                _DataRow["Year"] = value;
            }
        }
        #endregion

        #region int SumRating property
        public int SumRating
        {
            get
            {
                if (_DataRow["SumRating"] is int) return (int)_DataRow["SumRating"];
                return -1;
            }
            set
            {
                _DataRow["SumRating"] = value;
            }
        }
        #endregion

        #region int TotalRating property
        public int TotalRating
        {
            get
            {
                if (_DataRow["TotalRating"] is int) return (int)_DataRow["TotalRating"];
                return -1;
            }
            set
            {
                _DataRow["TotalRating"] = value;
            }
        }
        #endregion

        #region string ImageUrl property
        public string ImageUrl
        {
            get
            {
                if (_DataRow["ImageUrl"] is string) return (string)_DataRow["ImageUrl"];
                return "";
            }
            set
            {
                _DataRow["ImageUrl"] = value;
            }
        }
        #endregion

        #region string Director property
        public string Director
        {
            get
            {
                if (_DataRow["Director"] is string) return (string)_DataRow["Director"];
                return "";
            }
            set
            {
                _DataRow["Director"] = value;
            }
        }
        #endregion

        #region decimal Runtime property
        public decimal Runtime
        {
            get
            {
                if (_DataRow["Runtime"] is decimal) return (decimal)_DataRow["Runtime"];
                return -1;
            }
            set
            {
                _DataRow["Runtime"] = value;
            }
        }
        #endregion

        #region int CountryID property
        public int CountryID
        {
            get
            {
                if (_DataRow["CountryID"] is int) return (int)_DataRow["CountryID"];
                return -1;
            }
            set
            {
                _DataRow["CountryID"] = value;
            }
        }
        #endregion

        #region string Genre property
        public string Genre
        {
            get
            {
                if (_DataRow["Genre"] is string) return (string)_DataRow["Genre"];
                return "";
            }
            set
            {
                _DataRow["Genre"] = value;
            }
        }
        #endregion

        #region bool Status property
        public bool Status
        {
            get
            {
                if (_DataRow["Status"] is bool) return (bool)_DataRow["Status"];
                return true;
            }
            set
            {
                _DataRow["Status"] = value;
            }
        }
        #endregion

        public Movie(DataRow dataRow)
            : base(dataRow)
        {
        }
    }

    public class DanhMucMovie : DanhMucDoiTuong, IEnumerable
    {
        #region Instance property: Singleton Design pattern
        private static DanhMucMovie _Instance;
        public static DanhMucMovie Instance
        {
            get
            {
                //if (_Instance == null)
                //{
                    _Instance = new DanhMucMovie();
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                    _Instance.DataView.Sort = "";
                //}
                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "tblMovie";
            }
        }
        #endregion

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new Movie((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public Movie this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new Movie(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public Movie NewDoiTuong()
        {
            Movie doiTuong = new Movie(DataTable.NewRow());
            return doiTuong;
        }

        public Movie GetByProperty(string property, object value)
        {
            try
            {
                DataRow[] rows = GetDataRows(property, value);
                if (rows.Length > 0) return new Movie(rows[0]);
                else return null;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public Movie GetByID(int objID)
        {
            return GetByProperty("MovieID", objID);
        }
        public Movie GetByTen(string ten)
        {
            return GetByProperty("MovieName", ten);
        }

        public DanhMucMovie Filter(string filterExpression)
        {
            try
            {
                DanhMucMovie obj = new DanhMucMovie();
                obj.DataView = new DataView(DanhMucMovie.Instance.DataTable, filterExpression, "", DataViewRowState.CurrentRows);
                return obj;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public static DanhMucMovie GetAll()
        {
            try
            {
                return Instance;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        //Đầu ra là danh sách các user đã đánh giá phim đầu vào
        public static int[] GetUserIDDaDanhGiaByMovieID(int movieID)
        {
            SqlConnection con = new SqlConnection(DoiTuong._ConnectionString);
            try
            {
                con.Open();
                SqlCommand cmd = con.CreateCommand();

                cmd.CommandText = string.Format("SELECT DISTINCT TOP 10 UserID FROM tblRating WHERE MovieID = {0} ", movieID);

                SqlDataAdapter da = new SqlDataAdapter();
                DataTable dt = new DataTable();
                da.SelectCommand = cmd;
                da.Fill(dt);

                int[] result = new int[0];
                int i = 0;
                if (dt != null)
                {
                    result = new int[dt.Rows.Count];
                    foreach (DataRow row in dt.Rows)
                    {
                        if (row["UserID"] is int) result[i++] = (int)row["UserID"];
                    }
                }
                return result;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }

        public static int GetMaxID()
        {
            SqlConnection con = new SqlConnection(DoiTuong._ConnectionString);
            try
            {
                con.Open();
                SqlCommand cmd = con.CreateCommand();

                cmd.CommandText = string.Format("select MAX(MovieID) 'value' from tblMovie");

                SqlDataAdapter da = new SqlDataAdapter();
                DataTable dt = new DataTable();
                da.SelectCommand = cmd;
                da.Fill(dt);

                int result = 0;
                if (dt != null && dt.Rows.Count == 1)
                {
                    if (int.TryParse(dt.Rows[0]["value"].ToString(), out result))
                        return result;
                }
                return result;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }
    }
}